﻿using System;
using System.Net;
using System.Net.Sockets;
using Com.ZGH.Dns;

namespace DnsConsole
{
    class Program
    {
        static void Main(string[] args)
        {

            var listenUpdClieck = new UdpClient(53);

            var outGoingUdpClient = new UdpClient();

            while (true)
            {
                var remoteEp = new IPEndPoint(IPAddress.Any, 0);

                var dnsEp = new IPEndPoint(IPAddress.Parse("8.8.8.8"), 53);

                var question = listenUpdClieck.Receive(ref remoteEp);

                var a = DnsPacket.Parse(question);

                foreach (var x in a.Questions)
                {
                    Console.WriteLine("收到请求：" + x.Query.Query);
                }

                var bytes = a.ToBytes();

                outGoingUdpClient.Send(bytes, bytes.Length, dnsEp);

                var answer = outGoingUdpClient.Receive(ref dnsEp);

                var b = DnsPacket.Parse(answer);

                var c = b.ToBytes();

                listenUpdClieck.Send(c, c.Length, remoteEp);

                Console.WriteLine("Done");
            }
        }
    }
}
